﻿package chpt10;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Random;

/**
 * 该实现类运行需要MySql数据库test和表student
 * create table studentTest(
	id int not null primary key auto_increment,
	name varchar(20) not null
	);
	
 * @author zhrb
 *
 */
class Student{
	private int id;
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Student(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + "]";
	}
	
}

public class StudentDaoJDBCImpl {

	private static String URL = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=gbk";
	private static String driverName = "com.mysql.jdbc.Driver";
	private static String userName = "root";
	private static String password = "LU123456";
	
	public static boolean writeStudent(Student student) {
		String sql = "insert into student(name) values(?) ";//表中有id和name这列
		int result = -1;
		try(Connection conn = DriverManager.getConnection(URL, userName, password)) {
			Class.forName(driverName);
			PreparedStatement pstat = conn.prepareStatement(sql);
			pstat.setString(1, student.getName());
			result = pstat.executeUpdate();
		}catch (SQLException | ClassNotFoundException sqle) {
			sqle.printStackTrace();
		}
		return result>0?true:false;
	}

	public Student readStudent(String name) {
		ResultSet rs = null;
		String sql = "select * from student where name like ?";//表中有id和name这列
		Student student = null;
		try(Connection conn = DriverManager.getConnection(URL, userName, password)) {
			Class.forName(driverName);
			PreparedStatement pstat = conn.prepareStatement(sql);
			pstat.setString(1, name);
			rs = pstat.executeQuery();
			while(rs.next()){
				int stuId = rs.getInt("id");
				String stuName = rs.getString("name");
				student = new Student(stuId, stuName);
			}
		}catch (SQLException | ClassNotFoundException sqle) {
			sqle.printStackTrace();
		}
		return student;
	}

	public void diplayAllStudent() {
		String sql = "select * from student";//表中有id和name这列
		try(Connection conn = DriverManager.getConnection(URL, userName, password)) {
			Class.forName(driverName);
			Statement stat=conn.createStatement();
			ResultSet rs = stat.executeQuery(sql);
			while(rs.next()){
				int id = rs.getInt("id");
				String name = rs.getString("name");
				System.out.format("该学生的id=%d,姓名=%s\n",id,name);
			}
		}catch (SQLException | ClassNotFoundException sqle) {
			sqle.printStackTrace();
		}
	}

	public List<Student> getAllStudents() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public static String getRandomString(int length) { //length表示生成字符串的长度
	    String base = "abcdefghijklmnopqrstuvwxyz0123456789";   
	    Random random = new Random();   
	    StringBuffer sb = new StringBuffer();   
	    for (int i = 0; i < length; i++) {   
	        int number = random.nextInt(base.length());   
	        sb.append(base.charAt(number));   
	    }   
	    return sb.toString();   
	 }  
	
	public static void main(String[] args) {
		for (int i = 0; i < 1000; i++) {
			if(!writeStudent(new Student(i+1,getRandomString(20))))
				System.out.println(0);
		}
		
	}

}
